package com.hadron.media.fullsize_image 
{
	import temple.behaviors.IBehavior;
	import temple.core.CoreSprite;
	import temple.data.loader.preload.PreloaderMode;
	import temple.debug.IDebuggable;
	import temple.media.image.ImageLoader;
	import temple.ui.IHasBackground;
	import temple.ui.layout.ScaleMode;
	import temple.utils.StageProvider;

	import com.greensock.TweenMax;
	import com.greensock.easing.*;
	import com.hadron.media.fullsize_image.behaviors.FullsizeImageBehavior;

	import flash.events.Event;
	import flash.events.MouseEvent;
	/**
	 * ...
	 * @author Hadron
	 */
	public class FullsizeImage extends CoreSprite implements IDebuggable, IHasBackground
	{
		/*debuggable*/
		private var _debug:Boolean;
		/*image loader has-a*/
		private var _imageLoader:ImageLoader;
		/*behavior*/
		private var _behavior:IBehavior;
		/*background*/
		private var _background:Boolean;
		/*background alpha*/
		private var _backgroundAlpha:Number = 1;
		/*background color*/
		private var _backgroundColor:uint = 0;
	
		public function FullsizeImage()
		{
			super();
			init();
		}
		
		private function init():void
		{
			if (debug) logDebug('INITIALIZED');
			
			/*default settings for imageLoader*/
			_imageLoader = new ImageLoader();
			_imageLoader.cache = true;
			_imageLoader.preloaderMode = PreloaderMode.OWN_PRELOADER;
			_imageLoader.scaleMode = ScaleMode.NO_SCALE;
			//center image loader
			addChild(_imageLoader);
			_behavior = new FullsizeImageBehavior(_imageLoader);
			/*listen for mouse click*/
			this.addEventListener(MouseEvent.CLICK, handleMouseClick);
			this.buttonMode = true;
			this.addEventListener(Event.REMOVED_FROM_STAGE, handleRemove);
			this.addEventListener(Event.ADDED_TO_STAGE, handleAdd);
		}
		
		private function handleAdd(e:Event):void 
		{
			//removeEventListener(Event.ADDED_TO_STAGE, handleAdd);
			StageProvider.stage.addEventListener(Event.RESIZE, handleStageResize);
			this.buttonMode = true;
			handleStageResize();
			alpha = 0;
			TweenMax.to(this, .5, { alpha:1, ease:Quart.easeOut} );
		}
		
		private function handleRemove(e:Event):void 
		{
			//removeEventListener(Event.REMOVED_FROM_STAGE, handleRemove);
			StageProvider.stage.removeEventListener(Event.RESIZE, handleStageResize);
		}
		
		private function handleStageResize(e:Event = null):void 
		{
			//redraw background
			setBackground();
		}
		
		private function handleMouseClick(e:MouseEvent):void
		{
			//fade out then dispatch event
			TweenMax.to(this, .5, { alpha:0, ease:Quart.easeOut, onComplete:close} );
		}
		
		private function close():void
		{
			buttonMode = false;
			dispatchEvent(new Event(Event.CLOSE));
			StageProvider.stage.removeChild(this);
		}
		
		public function get imageLoader():ImageLoader
		{
			return _imageLoader;
		}
		
		/* INTERFACE temple.debug.IDebuggable */		
		public function get debug():Boolean
		{
			return _debug;
		}
		
		public function set debug(value:Boolean):void
		{
			_debug = value;
		}
		
		/* INTERFACE temple.ui.IHasBackground */
		public function set background(value:Boolean):void
		{
			_background = value;
			setBackground();
		}
		
		public function get background():Boolean
		{
			return _background;
		}
		
		public function set backgroundAlpha(value:Number):void
		{
			_backgroundAlpha = Math.min(Math.max(value, 0), 1);
			setBackground();
		}
		
		public function get backgroundAlpha():Number
		{
			return _backgroundAlpha;
		}
		
		public function set backgroundColor(value:uint):void
		{
			_backgroundColor = value;
			setBackground();
		}
		
		public function get backgroundColor():uint
		{
			return _backgroundColor;
		}
		
		private function setBackground():void
		{	
			if (debug) logDebug('redrawing background');
			var sWidth:uint = 10;
			var sHeight:uint = 10;
			if (StageProvider.stage)
			{
				sWidth = StageProvider.stage.stageWidth;
				sHeight = StageProvider.stage.stageHeight;
			}
			this.graphics.clear();
			if (this._background)
			{
				this.graphics.beginFill(this._backgroundColor, this._backgroundAlpha);
				this.graphics.drawRect(0, 0, sWidth, sHeight);
				this.graphics.endFill();
			}
		}
		
		override public function destruct():void
		{
			_imageLoader.destruct();
			_behavior = null;
			super.destruct();
		}
	}
}